package com.pqc.tbk.web.admin.account;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pqc.tbk.entity.account.User;
import com.pqc.tbk.service.admin.account.UserService;
import com.pqc.tbk.utils.JqGridSearchTo;
import com.pqc.tbk.utils.JsonMapper;
import com.pqc.tbk.utils.PageUtil;

@Controller
@RequestMapping("/admin/account")
public class AccountController {

	private UserService userService;
	// 分页工具
	private PageUtil<User> pageUtil;
	{
		pageUtil = new PageUtil<User>();
	}

	@RequestMapping("")
	public String login() {
		return "admin/account/login";
	}

	@RequestMapping("/{view}")
	public String left(@PathVariable("view") final String view) {
		return "admin/account/" + view;
	}

	/**
	 * @param _search
	 * @param searchTo
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/accList", method = RequestMethod.GET)
	@ResponseBody
	public String outList(final boolean _search, final JqGridSearchTo searchTo,
			final String state) {
		String jsonString = "";
		Page<User> page = null;
		final Pageable pageable = pageUtil.getPageable(searchTo.getPage(),
				searchTo.getRows());
		if (_search) {
			page = pageUtil.searchPage(searchTo, pageable, "", userService);
		} else {
			page = userService
					.getPage("" + searchTo.getOrderString(), pageable);
		}
		jsonString = pageToString(page);
		return jsonString;
	}

	/**
	 * 将page<User>对象转化成json格式的数据
	 * 
	 * @param page
	 * @return
	 */
	public String pageToString(final Page<User> page) {
		final List<User> list = page.getContent();
		final Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("total", page.getTotalPages());
		dataMap.put("page", page.getNumber());
		dataMap.put("records", page.getTotalElements());

		final List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

		for (final User item : list) {
			final Map<String, Object> row = new HashMap<String, Object>();
			row.put("id", item.getId());
			row.put("nick", item.getNick());
			row.put("username", item.getUsername());
			row.put("email", item.getEmail());
			row.put("tel", item.getTel());
			row.put("alipay", item.getAlipay());
			row.put("state", item.getState());
			row.put("role", item.getGroupNames());
			row.put("inviter_id", item.getInviter_id());
			rows.add(row);
		}
		dataMap.put("rows", rows);
		// 转json格式
		final JsonMapper jsonMapper = JsonMapper.buildNonDefaultMapper();
		final String json = jsonMapper.toJson(dataMap);
		return json;
	}

	@Autowired
	public void setUserService(final UserService userService) {
		this.userService = userService;
	}

}
